package com.gsmmodem;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.infomanage.po.Sendmsg;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class MsgSendServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String phonenums = request.getParameter("phonenum");
		String msgcontent = request.getParameter("msgcontent");
		String selectMsgType=request.getParameter("selectMsgType");
		
		String msgTypeString="";
		
		if (phonenums == null || phonenums.equals("") || msgcontent == null
				|| msgcontent.equals("")||selectMsgType.equals("")||selectMsgType==null) {
			response.sendRedirect("login.html");
		}
		if(selectMsgType.equals("normal")){
			msgTypeString="一般";
		}
		if(selectMsgType.equals("urgent")){
			msgTypeString="紧急";
		}
		System.out.println("msgTypeString="+msgTypeString);
		phonenums = new String(phonenums);
		String[] phonenum = phonenums.split(";");
		String s0 = new String(msgcontent);
		SessionFactory sf = new Configuration().configure()
				.buildSessionFactory();

		String headString="救护大队通知(";
		for (int i = 0; i < phonenum.length; i++) {
			System.out.println("phone num is:" + phonenum[i]);
			String msg="";
			 msg = phonenum[i].trim() + headString+msgTypeString+"):"+s0.trim();
  
			System.out.println("将要发送的消息是:" + msg);
			MsgSendClient.sendBuffer.add(msg);
			// Configuration cfg = new Configuration().configure();
			// //创建SessionFactory
			// SessionFactory factory = cfg.buildSessionFactory();
			// Session session = null;
			// session = factory.openSession();
			SimpleDateFormat formatter;
			formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String ctime = formatter.format(new Date());
			System.out.println("系统时间是:"+ctime);

			// 开启事务
			Session session = sf.openSession();
			session.beginTransaction();

			Sendmsg sendmsg = new Sendmsg();
			sendmsg.setMsgContent(s0);
			sendmsg.setPhoneNum(phonenum[i]);
			sendmsg.setSendTime(ctime);

			// 保存数据
			session.save(sendmsg);

			// 提交事务
			session.getTransaction().commit();
			session.close();

		}

		PrintWriter out = response.getWriter();
		StringBuffer scriptBuf = new StringBuffer();
		scriptBuf.append("<script type='text/javascript'>");
		scriptBuf.append("alert('OK');");
		scriptBuf.append("window.returnValue = 'ok';");
		scriptBuf.append("window.close();");
		scriptBuf.append("</script>");
		out.print(scriptBuf.toString());
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		System.out.print("msg send servlet is called!!!");
	}

	public static void main(String args[]) {
		String str = "13520330790;15901292663;13423687518";
		String num[] = str.split(";");
		for (int i = 0; i < num.length; i++) {
			System.out.println("num is" + num[i]);
		}
	}
}
